package com.zhuss.core.processor;

import java.io.IOException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.zxing.WriterException;
import com.zhuss.bean.bo.core.file.CreateFileBo;
import com.zhuss.bean.bo.core.upload.UploadQrcodeBo;
import com.zhuss.bean.vo.core.upload.UploadVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.QRCodeGenerator;

/**
 * qrcode
 * 文件上传  processor
 * @Author: zhushanshuo
 * @Date: 2020/02/25 17:55
 */
@Component("uploadQrcodeProcessor")
public class UploadQrcodeProcessor extends AbstractProcessor<UploadVo, UploadQrcodeBo> implements Processor<UploadVo, UploadQrcodeBo> {
	
	@Value("${fileReadPath}")
    private String fileReadPath;
	
	@Value("${filePath}")
    private String filePath;
	
	@Autowired
	@Qualifier("fileCreate")
	private Processor<Object, CreateFileBo> fileCreate;
	
    /**
     * 校验
     */
    @Override
    public void check(UploadQrcodeBo bo) throws OceanException {
    	
    }
    
    /**
     * 操作方法
     * @throws OceanException 
     */
    @Override
    public UploadVo process(UploadQrcodeBo bo) throws OceanException {
    	
    	String key = bo.getText() + ".png";
    	
    	try {
			QRCodeGenerator.generateQRCodeImage(bo.getText(), bo.getWidth(), bo.getHeight(), filePath + key);
		} catch (WriterException | IOException e) {
			e.printStackTrace();
			throw new OceanException(ExceptionCode.二维码错误);
		}
    	
    	UploadVo result = new UploadVo();
        result.setKey(key);
        result.setUrl(fileReadPath + key);
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public UploadVo log(UploadQrcodeBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.二维码上传.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName("文件key:" + param.getText());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
